package com.alibaba.dubbo.rpc.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.dubbo.common.HeaderProvider2;


/**
 * 客户端上下文
 * @author kingbo.ruan
 * @time 2014年9月27日 上午7:50:22
 */
public class Context implements HeaderProvider2{
	private Map<String, String> headers = new HashMap<String, String>();

	@Override
	public Map<String, String> getHeaders() {
		return headers;
	}
	
	public void setHeaders(Map<String, String> values){
		this.headers.clear();
		if(values != null){
			this.headers.putAll(values);
		}
	}

	@Override
	public String getHeader(String key) {
		return headers.get(key);
	}

	@Override
	public String getHeader(String key, String defaultValue) {
		String value = headers.get(key);
		if(value == null || value.length() == 0) return defaultValue;
		return value;
	}

	@Override
	public String removeHeader(String key) {
		return headers.remove(key);
	}

	@Override
	public void removeHeaders() {
		headers.clear();
	}
	
	/**
	 * 序列化器，缺省为2，hessian
	 */
	private int serializer = 2;

	public int getSerializer() {
		return serializer;
	}

	public void setSerializer(int serializer) {
		this.serializer = serializer;
	}
	
	/**
	 * 编解码器
	 */
	private List<String> messageCodec = new ArrayList<String>();

	public List<String> getMessageCodec() {
		return messageCodec;
	}

	public void setMessageCodec(List<String> messageCodec) {
		this.messageCodec = messageCodec;
	}
	
	/**
	 * 超时，毫秒
	 */
	private int timeout = 3000;

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	@Override
	public void setHeader(String key, String value) {
		this.headers.put(key, value);
	}

	@Override
	public boolean containsHeader(String key) {
		if(this.headers == null) return false;
		return this.headers.containsKey(key);
	}
}
